import Vue from 'vue'
import App from './App'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import VueRouter from 'vue-router'
import store from './vuex/store'
import Vuex from 'vuex'
import routes from './routes'
import axios from "axios";
import 'font-awesome/css/font-awesome.min.css'
axios.defaults.baseURL = "http://localhost:8081";
Vue.prototype.$axios = axios;
Vue.use(ElementUI)
Vue.use(VueRouter)
Vue.use(Vuex)
import BaiduMap from 'vue-baidu-map'
Vue.use(BaiduMap, {
  ak: 'R2WjCB7pvpXz6olVy5K58QcaMycWBfhi'
})
// 设置一个全局变量代表文件服务器地址和端口
Vue.prototype.FILE_SERVER = 'http://49.232.242.120:9998';

//NProgress.configure({ showSpinner: false });

const router = new VueRouter({
  routes
})
// 请求拦截器
axios.interceptors.request.use(config => {
  // 添加请求头 在请求头中添加token
  let token = localStorage.getItem('token');
  if (token) {
    config.headers['TOKEN'] = token;
  }
  return config;
}, error => {
  return Promise.reject(error);
});
// 响应拦截器
axios.interceptors.response.use(response => {
  // 如果登录过期则跳转到登录页面
  if (response.data.code !== 1 && response.data.msg === 'NOT_LOGIN') {
    // 跳转到登录页面并且提示信息
    Vue.prototype.$message.error("登录失效！请重新登录");
    router.push({ path: '/login' });
  }
  if (response.data.code !== 1 && response.data.msg === 'NO_AUTH') {
    // 跳转到登录页面并且提示信息
    Vue.prototype.$message.error("权限不足！");
    router.push({ path: '/401' });
  }
  return response;
});
router.beforeEach((to, from, next) => {
  if (to.path === '/register' || to.path === '/active') {
    next(); // 如果是注册页面，直接放行
  } else {
    let user = JSON.parse(localStorage.getItem('user'));
    if (!user && to.path !== '/login') {
      next({ path: '/login' })
    } else {
      next()
    }
  }
})
new Vue({
  //el: '#app',
  //template: '<App/>',
  router,
  store,
  //components: { App }
  render: h => h(App)
}).$mount('#app')

